<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="demo1">
    <div>{{ fullName }}</div>
    <button v-on:click="test1()">watch观察属性</button>
</div>
<div id="demo2">
    <div>{{ fullName }}</div>
    <button v-on:click="test2()">computed计算属性</button>
</div>

<script>
    var vm1 = new Vue({
        el: '#demo1',
        data: {
            firstName: 'Foo',
            lastName: 'Bar',
            fullName: 'Foo Bar'
        },
        methods:{
            test2:function () {
                this.firstName = this.firstName + 9;
            }
        },
        watch: {
            firstName: function (val) {
                this.fullName = val + ' ' + this.lastName
            },
            lastName: function (val) {
                this.fullName = this.firstName + ' ' + val
            }
        }
    })

    var vm2 = new Vue({
        el: '#demo2',
        data: {
            firstName: 'Foo',
            lastName: 'Bar'
        },
        computed: {
            fullName: function () {
                return this.firstName + ' ' + this.lastName
            }
        },
        methods:{
            test2:function () {
                this.firstName = this.firstName + 9;
            }
        }
    })
    // 上面代码是命令式且重复的。将它与计算属性的版本进行比较：好得多了，不是吗？
</script>
</body>
</html>
